import { fetchList, add, del, unlimit } from '@/services/campain/CampainSalerLink';
import { message } from 'antd';

export default {
  namespace: 'POperationCampainSalersSetting',

  state: {
    dataTable: {
      total: 0,
      list: [],
    },
  },

  effects: {
    * fetchList({ payload }, { call, put }) {
      const response = yield call(fetchList, payload);
      if (response.code === 0) {
        let { list } = response.data;
        list = list.map(d => {
          d.salerName = d.campainSaler.name ? d.campainSaler.name : '-';
          d.salerMobile = d.campainSaler.mobile ? d.campainSaler.mobile : '-';
          return d;
        });
        yield put({
          type: 'chgDataTable',
          payload: { ...response.data, list },
        });
      }
    },

    * add({ payload, callback }, { call }) {
      const response = yield call(add, payload);
      if (response.code === 0) {
        message.success('添加成功').then();
        callback && callback();
      }
    },

    * del({ payload, callback }, { call }) {
      const response = yield call(del, payload);
      if (response.code === 0) {
        message.success('删除成功').then();
        callback && callback();
      }
    },

    * unlimit({ payload, callback }, { call }) {
      message.loading('正在下载').then();
      const response = yield call(unlimit, payload);
      message.destroy();
      if (response.code === 0) {
        window.open(response.data.url, '_blank');
        callback && callback();
      }
    },

  },

  reducers: {
    chgDataTable(state, action) {
      return {
        ...state,
        dataTable: action.payload,
      };
    },
  },
};
